package com.benew.ntt.ydeval.sdk

import android.content.Context
import com.benew.ntt.evaluate.constant.EvalType
import com.benew.ntt.evaluate.listener.IEvalSDK
import com.benew.ntt.evaluate.sdk.EvalSDKConfig
import com.benew.ntt.evaluate.sdk.EvalSDKEntity
import com.benew.ntt.evaluate.sdk.EvalSDKHandler
import com.benew.ntt.evaluate.sdk.IEvalSDKListener
import com.benew.ntt.ydeval.SHttpClient
import com.benew.ntt.ydeval.SocketClient
import com.benew.ntt.ydeval.constant.YDEvalConst
import com.benew.ntt.ydeval.utils.EvalUtils
import com.ntt.core.nlogger.NLogger
import java.net.URI
import java.net.URISyntaxException
import java.util.*


/**
 * 有道口语评测管理器
 */
class YDEvalSDK : IEvalSDK {
    val TAG = javaClass.simpleName

    private var mEvalHandler: EvalSDKHandler? = null
    private var mSocketClient: SocketClient? = null
    private var mSHttpClient: SHttpClient? = null

    override fun init(context: Context,
                      evalListeners: MutableSet<IEvalSDKListener>?,
                      evalHandler: EvalSDKHandler?) {
        this.mEvalHandler = evalHandler
        evalListeners?.forEach {
            it.onReady()
        }

    }

    override fun startEval(entity: EvalSDKEntity) {
        try {
            if (EvalType.PHONETICIZE == entity.type) {
                mSHttpClient = SHttpClient(entity)
                mSHttpClient?.mSDKHandler = mEvalHandler
                mSHttpClient?.start()
            } else {
                val salt = UUID.randomUUID().toString()
                val curTime = System.currentTimeMillis() / 1000
                val signStr = YDEvalConst.APP_KEY + salt + curTime + YDEvalConst.APP_SECRET
                val sign = EvalUtils.getDigest(signStr)
                val format = "wav"
                val vadHeadSil = "3000" // 前置超时
                val vadTailSil = "2000" // 后置超时
                val langType = convertType(entity.type)
                val url =
                    YDEvalConst.REAL_TIME_URL + "appKey=" + YDEvalConst.APP_KEY + "&salt=" + salt + "&curtime=" + curTime +
                            "&sign=" + sign + "&signType=v4&langType=" + langType + "&format=" + format +
                            "&channel=1&version=v1&rate=16000&vadHeadSil=" + vadHeadSil + "&vadTailSil=" + vadTailSil
                NLogger.e(TAG, "startEval", url)
                mSocketClient = SocketClient(URI(url), entity.content!!)
                mSocketClient?.mSDKHandler = mEvalHandler
                mSocketClient?.start()
            }

        } catch (e: URISyntaxException) {
            e.printStackTrace()
            NLogger.e(TAG, "startEval 开启评测异常", e.message)
        }
    }

    override fun stopRecord() {
        mSocketClient?.stopRecord()
        mSHttpClient?.stopRecord()
    }

    override fun cancelEval() {
        mSocketClient?.releaseRes()
        mSHttpClient?.releaseRes()
        mSocketClient = null
        mSHttpClient = null
    }

    override fun deleteEval() {
        mEvalHandler = null
        mSocketClient = null
        mSHttpClient = null
    }

    override fun convertType(type: String?): String? {
        return when (type) {
            EvalType.CN_PRED -> {
                EvalType.ZH_CHS
            }
            EvalType.EN_WORD -> {
                EvalType.EN
            }
            else -> {
                type
            }
        }
    }

}